import { useUser } from "./../store/modules/user";
import request from "@/utils/request.ts";

interface ReqGetAiMessageList {
  page?: number;
  pageSize?: number;
  sessionId: number;
}

// 获取AI会话列表
export const GetAIMessageSessionList = () => {
  return request.get("/getAiSessionList");
};

// 创建AI会话
export const CreateAIMessageSession = () => {
  return request.get("/createAiSession");
};

// 删除AI会话
export const DeleteAIMessageSession = (sessionId: number) => {
  return request.delete("/deleteAiSession/" + sessionId);
};

// 获取AI会话消息
export const GetAIHistoryMessageList = (data: ReqGetAiMessageList) => {
  return request.post("/getAIMessageList", data);
};

// 请求AI接口
export const TryAskAi = async (data: Record<string, any>) => {
  const url = import.meta.env.VITE_Base + "/askAi";
  const { content, sessionId, signal } = data;
  let params = { content, sessionId };
  const useUserStore = useUser();

  const response = await fetch(url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-token": useUserStore.token!,
    },
    body: JSON.stringify(params),
    signal,
  });
  if (!response.ok) {
    throw new Error("Network response was not ok");
  }
  // 返回可读流
  return response;
};
